#define MSIFCONFIG_C
#include <string.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <stdio.h> 
#include <string.h> 
#include <errno.h> 
//set start flag
#include <libmscommon/mscommon.h>
#include <libmscommon/msnetwork.h>
#include <libmscommon/mscjson.h>
#include <libmscommon/msmd.h>
#include <libmslog/mslog.h>
#include "msperformance.h"
#include "msifconfig.h"

#define FLAG "IFCONFIG"

#define msifconfig_endline		LINE_END
#define msifconfig_file_ifupdown		"/etc/network/interfaces"
#define msifconfig_path_centos		"/etc/sysconfig/network-scripts"
#define msifconfig_file_netplan		"/etc/netplan/00-installer-config.yaml"
#define ms_dbmsifconfig  			"/etc/mscore/db/msdb_msifconfig.db"

static NETCARDInfo*pnetcard_info=&hardware_info.netcard_info;
static NETCARDInfo netcardinfo_db;

static ms_void msifconfig_innerapi_writedb(ms_void)
{
	cJSON * json_root = cJSON_CreateObject();
	if(ms_null == json_root){
		ms_errNoret("cJSON_CreateObject failed");
	}
	cJSON * json_msifconfiginfo_array = cJSON_CreateArray();
	cJSON_AddItemToObject(json_root, "msifconfiginfo", json_msifconfiginfo_array);
	ms_u32 eth_index=0;
	
	for(eth_index=0;eth_index<netcardinfo_db.num;eth_index++){
		cJSON * json_msifconfiginfo=cJSON_CreateObject();
		cJSON_AddItemToArray(json_msifconfiginfo_array,json_msifconfiginfo);	
		NETCFGInfo *pcfginfo=& netcardinfo_db.cfg_info[eth_index];
		MSJSON_BUILD_STRING(json_msifconfiginfo, "ethn_name", pcfginfo->name);
		MSJSON_BUILD_STRING(json_msifconfiginfo, "nameAnother", pcfginfo->nameAnother);
		MSJSON_BUILD_STRING(json_msifconfiginfo, "address", pcfginfo->address);
		MSJSON_BUILD_STRING(json_msifconfiginfo, "ipaddress", pcfginfo->ipaddress);
		MSJSON_BUILD_STRING(json_msifconfiginfo, "netmask", pcfginfo->netmask);
		MSJSON_BUILD_STRING(json_msifconfiginfo, "gateway", pcfginfo->gateway);
		MSJSON_BUILD_STRING(json_msifconfiginfo, "mtu", pcfginfo->mtu);
		MSJSON_BUILD_STRING(json_msifconfiginfo, "bondmaster_name", pcfginfo->bondmaster_name);
		MSJSON_BUILD_STRING(json_msifconfiginfo, "dns1", pcfginfo->dns1);
		MSJSON_BUILD_STRING(json_msifconfiginfo, "dns2", pcfginfo->dns2);
		MSJSON_BUILD_STRING(json_msifconfiginfo, "mode", pcfginfo->mode);
		cJSON_AddNumberToObject(json_msifconfiginfo, "flag_defaultgw", pcfginfo->flag_defaultgw);
		if(ms_true==pcfginfo->flag_defaultgw){
			netcardinfo_db.num_defaultroute+=1;
		}
	}	
	char * json_string = cJSON_Print(json_root);
	cJSON_Delete(json_root);
	if(ms_null==json_string){
		ms_errNoret("cJSON_Print failed");
	}
	msmd_api_writeProtect(ms_dbmsifconfig, json_string, strlen(json_string));
ms_end:	
	free(json_string);
}

static ms_s08 msifconfig_innerapi_readdb(ms_void)
{
	ms_byte json_buf[40960]={0};
	if(0>msmd_api_read2(ms_dbmsifconfig, json_buf, sizeof(json_buf))){
		msifconfig_innerapi_writedb();
		ms_errRet(-1,"msmd_api_read2 faild");
	}
	cJSON *jsonroot = cJSON_Parse(json_buf);
	if(!jsonroot) {
		ms_errRet(-1,"cJSON_Parse:get root faild");
	}
	ms_string ethmod[128]={ethnmode_string};
	cJSON *json_msifconfiginfo_array=cJSON_GetObjectItem(jsonroot,"msifconfiginfo");
	int arrySize=cJSON_GetArraySize(json_msifconfiginfo_array); 
	cJSON *json_msifconfiginfo_list=json_msifconfiginfo_array->child;
	cJSON *js_item=ms_null;
	int ret=0;
	while(json_msifconfiginfo_list!=ms_null)  {  
		ms_byte ethn_name[128]; 
		MSJSON_PARSE_STRING(js_item, json_msifconfiginfo_list, "ethn_name", ethn_name);
		ms_u08 netcard_index=0;
		for(netcard_index=0;netcard_index<netcardinfo_db.num;netcard_index++){
			NETCFGInfo *pcfginfo=& netcardinfo_db.cfg_info[netcard_index];
			if(ms_strncmp_saeq(pcfginfo->name, ethn_name)){
				MSJSON_PARSE_STRING_DEFALUT(js_item, json_msifconfiginfo_list,"nameAnother", pcfginfo->nameAnother,ethn_name);
				MSJSON_PARSE_STRING_BUFINIT(js_item, json_msifconfiginfo_list, "gateway", pcfginfo->gateway);
				MSJSON_PARSE_STRING_BUFINIT(js_item, json_msifconfiginfo_list, "mtu", pcfginfo->mtu);
				MSJSON_PARSE_STRING_BUFINIT(js_item, json_msifconfiginfo_list, "bondmaster_name", pcfginfo->bondmaster_name);
				MSJSON_PARSE_STRING_BUFINIT(js_item, json_msifconfiginfo_list, "bondslave_name", pcfginfo->bondslave_name);
				MSJSON_PARSE_STRING_BUFINIT(js_item, json_msifconfiginfo_list, "dns1", pcfginfo->dns1);
				MSJSON_PARSE_STRING_BUFINIT(js_item, json_msifconfiginfo_list, "dns2", pcfginfo->dns2);
				MSJSON_PARSE_STRING_BUFINIT(js_item, json_msifconfiginfo_list, "mode", pcfginfo->mode);
				MSJSON_PARSE_INT(js_item, json_msifconfiginfo_list, "flag_defaultgw", pcfginfo->flag_defaultgw);
				break;
			}
		}
		json_msifconfiginfo_list=json_msifconfiginfo_list->next; 
	}  
	return 0;
}

ms_bool msifconfig_innerapi_updateData(ms_void)
{
	return msifconfig_api_init();
}
ms_bool msifconfig_innerapi_check(NETCFGInfo *pcfg_info,ms_u08 * ms_out  poutstr)
{
	switch(msnet_api_getIpMode(pcfg_info->mode)){	////0-static ;1-hdcp;2-bond-master;3-bond-slaver
		case netcfg_mode_static:
			if(!(ms_buflen(pcfg_info->name) >0
				&&ms_buflen(pcfg_info->ipaddress) >0
				&&ms_buflen(pcfg_info->netmask) >0	)	){
				ms_sprintfs(poutstr, "[%s] must set name,ipaddress and netmask in static mode",pcfg_info->name);
				ms_errRet(ms_false,"%s",poutstr);
			}
			break;
		case netcfg_mode_dhcp:
			if(!(ms_buflen(pcfg_info->name) >0)){
				ms_sprintfs(poutstr, "[%s] must set name in dhcp mode",pcfg_info->name);
				ms_errRet(ms_false,"%s",poutstr);
			}
			break;
		case netcfg_mode_bondslaver:
			if(!(ms_buflen(pcfg_info->name) >0
				&&ms_buflen(pcfg_info->bondmaster_name) >0)	){
				ms_sprintfs(poutstr, "[%s ]must set name and bondmaster_name in bond-slaver mode",pcfg_info->name);
				ms_errRet(ms_false,"%s",poutstr);
			}
			break;
		case netcfg_mode_bondmaster:		
			if(!(ms_buflen(pcfg_info->name) >0
				&&ms_buflen(pcfg_info->ipaddress) >0
				&&ms_buflen(pcfg_info->netmask) >0
				&&ms_buflen(pcfg_info->bondslave_name) >0)){
				ms_sprintfs(poutstr, "[%s ]must set name,ipaddress,netmask and bondslave_name in bond-master mode",pcfg_info->name);
				ms_errRet(ms_false,"%s",poutstr);
			}
			break;	
	}

	if((ms_buflen(pcfg_info->address)>0)
		&&(ms_false==msnet_api_isVaildMac(pcfg_info->address))){
		ms_sprintfs(poutstr, "[%s ]the first digit of a MAC address must be an even number ",pcfg_info->name);
		ms_errRet(ms_false,"%s",poutstr);
	}
	return ms_true;
}


ms_bool msifconfig_innerapi_checkDefaultGW(ms_u08 * ms_out  poutstr)
{
	ms_u08 netcard_index=0;
	ms_u08 num_defaultgw=0;
	ms_u08 defaultgw_netcard_index[msnet_maxnum_iface];
	ms_byte tbuf[256]={0};
	ms_byte rthnlist[256]={0};
	for(netcard_index=0;netcard_index<netcardinfo_db.num;netcard_index++){
		NETCFGInfo *pcfg_info=&netcardinfo_db.cfg_info[netcard_index];
		if(ms_true==pcfg_info->flag_defaultgw){
			defaultgw_netcard_index[num_defaultgw]=netcard_index;
			ms_strcats(tbuf, rthnlist, "%s,", pcfg_info->name);
			num_defaultgw++;
			if(ms_buflen(pcfg_info->gateway)==0){
				ms_sprintfs(poutstr, "Set defalutgw,but no set gateway");
				ms_errRet(ms_false,"%s",poutstr);
			}
		}
		if(num_defaultgw>1){
			ms_strcpy(poutstr, rthnlist);
			ms_errRet(ms_false,"Num of defalutgw > 1,rthnlist: %s",rthnlist);
		}
	}
	return ms_true;
}

ms_bool msifconfig_innerapi_needSet(NETCFGInfo *pcfg_info1,NETCFGInfo *pcfg_info2)
{
	ms_verbose("pcfg_info1:name-%s,address-%s,ipaddress-%s,netmask-%s,gateway-%s,bondmaster_name-%s,bondslave_name-%s,dns1-%s,dns2-%s,mode-%s,flag_defaultgw-%d",
		pcfg_info1->name,pcfg_info1->address,pcfg_info1->ipaddress,pcfg_info1->netmask,
		pcfg_info1->gateway, pcfg_info1->bondmaster_name, pcfg_info1->bondslave_name, pcfg_info1->dns1, pcfg_info1->dns2, pcfg_info1->mode,pcfg_info1->flag_defaultgw);
	ms_verbose("pcfg_info2:name-%s,address-%s,ipaddress-%s,netmask-%s,gateway-%s,bondmaster_name-%s,bondslave_name-%s,dns1-%s,dns2-%s,mode-%s,flag_defaultgw-%d",
		pcfg_info2->name,pcfg_info2->address,pcfg_info2->ipaddress,pcfg_info2->netmask,
		pcfg_info2->gateway, pcfg_info2->bondmaster_name, pcfg_info2->bondslave_name, pcfg_info2->dns1, pcfg_info2->dns2, pcfg_info2->mode,pcfg_info2->flag_defaultgw);
	
	return (ms_strncmp_saeq(pcfg_info1->nameAnother, pcfg_info2->nameAnother)
			&&ms_strncmp_saeq(pcfg_info1->name, pcfg_info2->name)
			&&ms_strncmp_saeq(pcfg_info1->address, pcfg_info2->address)
			&&ms_strncmp_saeq(pcfg_info1->ipaddress, pcfg_info2->ipaddress)
			&&ms_strncmp_saeq(pcfg_info1->netmask, pcfg_info2->netmask)
			&&ms_strncmp_saeq(pcfg_info1->gateway, pcfg_info2->gateway)
			&&ms_strncmp_saeq(pcfg_info1->bondmaster_name, pcfg_info2->bondmaster_name)
			&&ms_strncmp_saeq(pcfg_info1->bondslave_name, pcfg_info2->bondslave_name)
			&&ms_strncmp_saeq(pcfg_info1->dns1, pcfg_info2->dns1)
			&&ms_strncmp_saeq(pcfg_info1->dns2, pcfg_info2->dns2)
			&&ms_strncmp_saeq(pcfg_info1->mode, pcfg_info2->mode)
			&&(pcfg_info1->flag_defaultgw==pcfg_info2->flag_defaultgw)
		) ? ms_false : ms_true;
}

ms_s08 msifconfig_innerapi_setIfupdown(NETCARDInfo* ms_in pnetcardinfo,ms_u08 * ms_out  poutstr)
{

	char  strbuf[2048]={0};
	char  strbuf_tmp[2048]={0};	
	ms_strcats(strbuf_tmp, strbuf, "%s",
		"auto lo"msifconfig_endline
		"    iface lo inet loopback"msifconfig_endline);
	ms_u08 netcard_index=0;
	for(netcard_index=0;netcard_index<netcardinfo_db.num;netcard_index++){
		NETCFGInfo *pcfg_info=&netcardinfo_db.cfg_info[netcard_index];
		ms_verbose("------name:%s,mode:%s,need_restart:%d",pcfg_info->name,pcfg_info->mode,pcfg_info->flag_needRestart);
		ms_byte check_outstr[1024]={0};
		if(ms_false==msifconfig_innerapi_check( pcfg_info,check_outstr)){
			continue;
		}
		switch(msnet_api_getIpMode(pcfg_info->mode)){	////0-static ;1-hdcp;2-bond-master;3-bond-slaver
			case netcfg_mode_static:
				ms_strcats(strbuf_tmp, strbuf, 
					"auto %s"msifconfig_endline
					"   iface %s inet static"msifconfig_endline
					"   address %s"msifconfig_endline
					"   netmask %s"msifconfig_endline, 
					pcfg_info->name,
					pcfg_info->name,
					pcfg_info->ipaddress,
					pcfg_info->netmask);
	
				if((ms_true==pcfg_info->flag_defaultgw)
					&&ms_buflen(pcfg_info->gateway)>0){
					ms_strcats(strbuf_tmp, strbuf, 
						"   gateway %s"msifconfig_endline, 
						pcfg_info->gateway);
				}
				
				if(ms_buflen(pcfg_info->address)>0){
					ms_strcats(strbuf_tmp, strbuf, 
						"   hwaddress ether %s"msifconfig_endline, 
						pcfg_info->address);
				}
				if(ms_buflen(pcfg_info->dns1)>0){
					ms_strcats(strbuf_tmp, strbuf, 
						"   dns-nameservers %s"msifconfig_endline, 
						pcfg_info->dns1);
				}
				if(ms_buflen(pcfg_info->dns2)>0){
					ms_strcats(strbuf_tmp, strbuf, 
						"   dns-nameservers %s"msifconfig_endline, 
						pcfg_info->dns2);
				}
				break;
			case netcfg_mode_dhcp:
				ms_strcats(strbuf_tmp, strbuf, 
					"auto %s"msifconfig_endline
					"    iface %s inet dhcp"msifconfig_endline, 
					pcfg_info->name,pcfg_info->name
				);
				if(ms_buflen(pcfg_info->address)>0){
					ms_strcats(strbuf_tmp, strbuf, "   hwaddress ether %s"msifconfig_endline, pcfg_info->address);
				}
				break;
			case netcfg_mode_bondslaver:
				ms_strcats(strbuf_tmp, strbuf, 
					"auto %s"msifconfig_endline
					"    iface %s inet manual"msifconfig_endline
					"    bond-master %s"msifconfig_endline,
					pcfg_info->name,
					pcfg_info->name,
					pcfg_info->bondmaster_name);
				break;
			case netcfg_mode_bondmaster:		
				ms_strcats(strbuf_tmp, strbuf, 
					"auto %s"msifconfig_endline
					"   iface %s inet static"msifconfig_endline
					"   address %s"msifconfig_endline
					"   netmask %s"msifconfig_endline
					"   bond-mode 5"msifconfig_endline
					"   bond-miimon 100"msifconfig_endline
					"   bond-slaves none"msifconfig_endline, 
					pcfg_info->name,
					pcfg_info->name,
					pcfg_info->ipaddress,
					pcfg_info->netmask);	
				if((ms_true==pcfg_info->flag_defaultgw)
					&&ms_buflen(pcfg_info->gateway)>0){
					ms_strcats(strbuf_tmp, strbuf, 
						"   gateway %s"msifconfig_endline, 
						pcfg_info->gateway);
				}
				if(ms_buflen(pcfg_info->dns1)>0){
					ms_strcats(strbuf_tmp, strbuf, 
						"   dns-nameservers %s"msifconfig_endline, 
						pcfg_info->dns1);
				}
				if(ms_buflen(pcfg_info->dns2)>0){
					ms_strcats(strbuf_tmp, strbuf, 
						"   dns-nameservers %s"msifconfig_endline, 
						pcfg_info->dns2);
				}
				break;	
		}
	#if 0	
		if(ms_strncmp_saeq(pcfg_info->name, "eth0")){
			ms_strcats(strbuf_tmp, strbuf, 
				"auto %s"msifconfig_endline
				"   iface %s inet static"msifconfig_endline
				"   address %s"msifconfig_endline
				"   netmask %s"msifconfig_endline, 
				"eth0:1",
				"eth0:1",
				"10.10.200.254",
				"255.255.255.0");
		}
	#endif
	}
	if(msmd_api_writeProtect(msifconfig_file_ifupdown, strbuf, ms_buflen(strbuf))<0){
		ms_sprintfs(poutstr, "%s","write configfile error");
		ms_errRet(-1,"%s",poutstr);
	}
	return 0;
}
ms_s08 msifconfig_innerapi_setNetplan(NETCARDInfo* ms_in pnetcardinfo,ms_u08 * ms_out  poutstr)
{

	char  strbuf[2048]={0};
	char  strbuf_tmp[2048]={0};	
	ms_strcats(strbuf_tmp, strbuf, "%s",
		"# This is the network config written by 'msavskit'"msifconfig_endline
		"network:"msifconfig_endline
		"  version: 2"msifconfig_endline
		"  ethernets:"msifconfig_endline);
	ms_u08 netcard_index=0;
	for(netcard_index=0;netcard_index<netcardinfo_db.num;netcard_index++){
		NETCFGInfo *pcfg_info=&netcardinfo_db.cfg_info[netcard_index];
		ms_verbose("------name:%s,mode:%s,need_restart:%d",pcfg_info->name,pcfg_info->mode,pcfg_info->flag_needRestart);
		ms_byte check_outstr[1024]={0};
		if(ms_false==msifconfig_innerapi_check( pcfg_info,check_outstr)){
			continue;
		}
		ms_u08 netmaskbit=msnet_api_getNetmaskBitNum(pcfg_info->netmask);
		switch(msnet_api_getIpMode(pcfg_info->mode)){	////0-static ;1-hdcp;2-bond-master;3-bond-slaver
			case netcfg_mode_static:
				ms_strcats(strbuf_tmp, strbuf, 
					"    %s:"msifconfig_endline
					"      addresses:"msifconfig_endline
					"      - %s/%d"msifconfig_endline, 
					pcfg_info->name,
					pcfg_info->ipaddress,
					netmaskbit);
		#if 0
				if(ms_strncmp_saeq(pcfg_info->name, "eth0")){
					ms_strcats(strbuf_tmp, strbuf, 
					"      - 10.10.200.254/24"msifconfig_endline);
				}
		#endif
				if((ms_true==pcfg_info->flag_defaultgw)&&ms_buflen(pcfg_info->gateway)>0){
					ms_strcats(strbuf_tmp, strbuf, 
					"      routes:"msifconfig_endline
					"      - to: default"msifconfig_endline
					"        via: %s"msifconfig_endline,pcfg_info->gateway);
				}
				
				if(ms_buflen(pcfg_info->address)>0){
					ms_strcats(strbuf_tmp, strbuf, 
					"      macaddress: %s"msifconfig_endline, pcfg_info->address);
				}
				if((ms_buflen(pcfg_info->dns1)>0)||(ms_buflen(pcfg_info->dns2)>0)){
					ms_strcats(strbuf_tmp, strbuf, 
					"      nameservers:"msifconfig_endline,
					"        addresses:"msifconfig_endline);
					if(ms_buflen(pcfg_info->dns1)>0){
					ms_strcats(strbuf_tmp, strbuf, 
					"        - %s"msifconfig_endline, pcfg_info->dns1);
					}
					if(ms_buflen(pcfg_info->dns2)>0){
					ms_strcats(strbuf_tmp, strbuf, 
					"        - %s"msifconfig_endline, pcfg_info->dns2);
					}
				}
				break;
			case netcfg_mode_dhcp:
				ms_strcats(strbuf_tmp, strbuf, 
					"    %s:"msifconfig_endline
					"      dhcp4: true"msifconfig_endline);
				break;
			case netcfg_mode_bondslaver:
				ms_strcats(strbuf_tmp, strbuf, 
					"    %s:{}"msifconfig_endline,
					pcfg_info->name);
				break;
			case netcfg_mode_bondmaster:	
				ms_strcats(strbuf_tmp, strbuf, 
					"  bonds: "msifconfig_endline
					"    %s: "msifconfig_endline
					"      addresses:"msifconfig_endline
					"      - %s/%d"msifconfig_endline, 
					pcfg_info->name,
					pcfg_info->ipaddress,
					netmaskbit);	
				if((ms_true==pcfg_info->flag_defaultgw)
					&&ms_buflen(pcfg_info->gateway)>0){
					ms_strcats(strbuf_tmp, strbuf, 
					"      routes:"msifconfig_endline
					"      - to: default"msifconfig_endline
					"        via: %s"msifconfig_endline,pcfg_info->gateway);
				}
				if((ms_buflen(pcfg_info->dns1)>0)||(ms_buflen(pcfg_info->dns2)>0)){
					ms_strcats(strbuf_tmp, strbuf, 
					"      nameservers:"msifconfig_endline
					"        addresses:"msifconfig_endline);
					if(ms_buflen(pcfg_info->dns1)>0){
					ms_strcats(strbuf_tmp, strbuf, 
					"        - %s"msifconfig_endline, pcfg_info->dns1);
					}
					if(ms_buflen(pcfg_info->dns2)>0){
					ms_strcats(strbuf_tmp, strbuf, 
					"        - %s"msifconfig_endline, pcfg_info->dns2);
					}
				}
				ms_strcats(strbuf_tmp, strbuf, 
					"      interfaces: [%s]"msifconfig_endline, pcfg_info->bondslave_name);
				break;	
		}
	}
	if(msmd_api_writeProtect(msifconfig_file_netplan, strbuf, ms_buflen(strbuf))<0){
		ms_sprintfs(poutstr, "%s","write configfile error");
		ms_errRet(-1,"%s",poutstr);
	}
	return 0;
}
ms_s08 msifconfig_innnerapi_setRhel(NETCARDInfo* ms_in pnetcardinfo,ms_u08 * ms_out  poutstr)
{
	char  strbuf[2048]={0};
	char  strbuf_tmp[2048]={0};	
	ms_u08 netcard_index=0;
	for(netcard_index=0;netcard_index<netcardinfo_db.num;netcard_index++){
		NETCFGInfo *pcfg_info=&netcardinfo_db.cfg_info[netcard_index];
		ms_verbose("------name:%s,mode:%s,need_restart:%d",pcfg_info->name,pcfg_info->mode,pcfg_info->flag_needRestart);
		switch(msnet_api_getIpMode(pcfg_info->mode)){	////0-static ;1-hdcp;2-bond-master;3-bond-slaver
			case netcfg_mode_static:
				ms_strcats(strbuf_tmp, strbuf, 
					"DEVICE=%s"msifconfig_endline
					"ONBOOT=yes"msifconfig_endline
					"BOOTPROTO=static"msifconfig_endline
					"IPADDR=%s"msifconfig_endline
					"NETMASK=%s"msifconfig_endline, 
					pcfg_info->name,
					pcfg_info->ipaddress,
					pcfg_info->netmask);
	
				if((ms_true==pcfg_info->flag_defaultgw)&&ms_buflen(pcfg_info->gateway)>0){
					ms_strcats(strbuf_tmp, strbuf, "GATEWAY=%s"msifconfig_endline, pcfg_info->gateway);
				}
				if(ms_buflen(pcfg_info->address)>0){
					ms_strcats(strbuf_tmp, strbuf, "HWADDR=%s"msifconfig_endline, pcfg_info->address);
				}
				if(ms_buflen(pcfg_info->dns1)>0){
					ms_strcats(strbuf_tmp, strbuf, "DNS1=%s"msifconfig_endline, pcfg_info->dns1);
				}
				if(ms_buflen(pcfg_info->dns2)>0){
					ms_strcats(strbuf_tmp, strbuf, "DNS2=%s"msifconfig_endline, pcfg_info->dns2);
				}
				break;
			case netcfg_mode_dhcp:
				ms_strcats(strbuf_tmp, strbuf, 
					"DEVICE=%s"msifconfig_endline
					"ONBOOT=yes"msifconfig_endline
					"BOOTPROTO=dhcp"msifconfig_endline,
					pcfg_info->name);
				if(ms_buflen(pcfg_info->address)>0){
					ms_strcats(strbuf_tmp, strbuf, "HWADDR=%s"msifconfig_endline, pcfg_info->address);
				}
				break;
			case netcfg_mode_bondslaver:

				break;
			case netcfg_mode_bondmaster:		
				
				break;	
		}

		ms_byte msifconfig_file[512]={0};
		ms_sprintfs(msifconfig_file, "%s/ifcfg-eth%d", msifconfig_path_centos,netcard_index);
		if(msmd_api_writeProtect(msifconfig_file, strbuf, ms_buflen(strbuf))<0){
			ms_sprintfs(poutstr, "%s","write configfile error");
			ms_errRet(-1,"%s",poutstr);
		}
	}
	return 0;
}
ms_s08 msifconfig_innerapi_restartIfupdown(NETCFGInfo *pcfg_info,ms_u08 * ms_out  poutstr)
{
	if(ms_false==msmd_api_isInstallByBin("ip")){
		return -1;
	}
	ms_s08 ret =0;
	ms_s08 cmdline[256]={0};
	ms_sprintfs(cmdline, "ip addr flush dev %s",pcfg_info->name);	
	ms_verbose("cmdline:%s", cmdline);
	if(ms_false==ms_cmdRet(cmdline)){
		ms_sleep(1);
		if(ms_false==ms_cmdRet(cmdline)){
			ret=-1;
			ms_sprintfs(poutstr, "[%s]ip addr flush failed",pcfg_info->name);
			ms_errGoto(ms_end,"%s", poutstr);
		}
	}

	ms_sprintfs(cmdline, "ifdown %s",pcfg_info->name);
	ms_verbose("cmdline:%s", cmdline);
	if(ms_false==ms_cmdRet(cmdline)){
		ms_sleep(1);
		if(ms_false==ms_cmdRet(cmdline)){
			ms_sprintfs(poutstr, "[%s]ifdown failed",pcfg_info->name);
			ret=-2;
			ms_errGoto(ms_end, "%s", poutstr);
		}
	}
			
	ms_sprintfs(cmdline,  "ifup %s",pcfg_info->name);	
	ms_verbose("cmdline:%s", cmdline);
	if(ms_false==ms_cmdRet(cmdline)){
		ms_sleep(1);
		if(ms_false==ms_cmdRet(cmdline)){
			ms_sprintfs(poutstr, "[%s]ifup failed",pcfg_info->name);
			ret=-3;
			ms_errGoto(ms_end, "%s", poutstr);
		}
	}
ms_end:
	return ret;
}
ms_s08 msifconfig_innerapi_restartNetplan(ms_u08 * ms_out  poutstr)
{
	ms_s08 ret =0;
	ms_byte cmdline[256]={"netplan apply"};
	ms_verbose("cmdline:%s", cmdline);
	if(ms_false==ms_cmdRet(cmdline)){
		ms_sleep(1);
		if(ms_false==ms_cmdRet(cmdline)){
			ret=-4;
			ms_sprintfs(poutstr, "%s failed",cmdline);
			ms_errGoto(ms_end,"%s", poutstr);
		}
	}
ms_end:
	return ret;
}
/*初始化netcardinfo_db的参数，并同步给pnetcard_info*/
ms_bool msifconfig_api_init(ms_void)
{
	//由于网卡配置前必须获取到网卡的信息
	if(ms_false==pnetcard_info->isinit){
		ms_errRet(ms_false,"%s","Init pnetcard_info first");
	}
	//从系统获取中获取MAC,MTU,IP地址,掩码,网关
	mspmc_api_netInfo(pnetcard_info);
	ms_memset(&netcardinfo_db, 0, sizeof(NETCARDInfo));
	//将从系统获取的参数同步给netcardinfo_db
	ms_memcpy(&netcardinfo_db, pnetcard_info, sizeof(NETCARDInfo));
	
	//从配置文件中获取无法从系统中读取的一些参数
	msifconfig_innerapi_readdb();

	//将从配置文件中获取的参数同步给pnetcard_info
	ms_u32 eth_index=0;
	for(eth_index=0;eth_index<pnetcard_info->num;eth_index++){
		ms_strcpy(pnetcard_info->cfg_info[eth_index].nameAnother, netcardinfo_db.cfg_info[eth_index].nameAnother);
		ms_strcpy(pnetcard_info->cfg_info[eth_index].gateway, netcardinfo_db.cfg_info[eth_index].gateway);
		ms_strcpy(pnetcard_info->cfg_info[eth_index].bondmaster_name, netcardinfo_db.cfg_info[eth_index].bondmaster_name);
		ms_strcpy(pnetcard_info->cfg_info[eth_index].bondslave_name, netcardinfo_db.cfg_info[eth_index].bondslave_name);
		ms_strcpy(pnetcard_info->cfg_info[eth_index].dns1, netcardinfo_db.cfg_info[eth_index].dns1);
		ms_strcpy(pnetcard_info->cfg_info[eth_index].dns2, netcardinfo_db.cfg_info[eth_index].dns2);
		pnetcard_info->cfg_info[eth_index].flag_defaultgw=netcardinfo_db.cfg_info[eth_index].flag_defaultgw;
		if(0==ms_buflen(netcardinfo_db.cfg_info[eth_index].mode)){
			ms_string ethmod[128]={ethnmode_string};
			ms_debug("[%s] mode is 0,set to %s",netcardinfo_db.cfg_info[eth_index].name, ethmod[netcfg_mode_static]);
			ms_strcpy(netcardinfo_db.cfg_info[eth_index].mode, ethmod[netcfg_mode_static]);
		}
		ms_strcpy(pnetcard_info->cfg_info[eth_index].mode, netcardinfo_db.cfg_info[eth_index].mode);
	}
	netcardinfo_db.isinit=ms_true;
	return ms_true;
}
ms_s08 msifconfig_api_set(NETCARDInfo* ms_in pnetcardinfo,ms_u08 * ms_out  poutstr)
{
	if(ms_false==pnetcard_info->isinit||ms_false==netcardinfo_db.isinit){
		ms_sprintfs(poutstr, "%s","Init pnetcard_info and netcardinfo_db first");
		ms_errRet(-1,"%s",poutstr);
	}
	//获取最新的网卡信息
	msifconfig_innerapi_updateData();
	
	ms_u08 netcard_index=0;
	ms_u08 index=0;
	ms_u08 right_num=0;
	ms_u08 bond_index=netcardinfo_db.num;
	for(index=0;index<pnetcardinfo->num;index++){
		ms_verbose("pnetcardinfo------------name:%s,mode:%s",pnetcardinfo->cfg_info[index].name,pnetcardinfo->cfg_info[index].mode);
		for(netcard_index=0;netcard_index<netcardinfo_db.num;netcard_index++){
			if(netcfg_mode_bondmaster==msnet_api_getIpMode(pnetcardinfo->cfg_info[index].mode)){
				ms_memset(&netcardinfo_db.cfg_info[bond_index], 0, sizeof(NETCFGInfo));	
				ms_memcpy(&netcardinfo_db.cfg_info[bond_index], &pnetcardinfo->cfg_info[index], sizeof(NETCFGInfo));
				netcardinfo_db.cfg_info[bond_index].flag_needRestart=ms_true;
				right_num++;
				bond_index++;
				break;
			}else if(ms_strncmp_saeq(netcardinfo_db.cfg_info[netcard_index].name, pnetcardinfo->cfg_info[index].name)){
				//检测参数是否足够
				if(ms_false==msifconfig_innerapi_check( &pnetcardinfo->cfg_info[index],poutstr)){
					ms_errRet(-2,"msifconfig_innerapi_check failed");
				}
				//重复的参数，不再重新设置
				if(ms_true==msifconfig_innerapi_needSet(&netcardinfo_db.cfg_info[netcard_index], &pnetcardinfo->cfg_info[index])){
					ms_memset(&netcardinfo_db.cfg_info[netcard_index], 0, sizeof(NETCFGInfo));	
					ms_memcpy(&netcardinfo_db.cfg_info[netcard_index], &pnetcardinfo->cfg_info[index], sizeof(NETCFGInfo));	
					netcardinfo_db.cfg_info[bond_index].flag_needRestart=ms_true;
				}else{
					ms_errRet(-4,"[%s] config no change ,no need to set",netcardinfo_db.cfg_info[netcard_index].name);
				}
				right_num++;
				break;
			}
		}
		if(!(netcard_index<netcardinfo_db.num)){
			ms_sprintfs(poutstr,"there is no iface %s",pnetcardinfo->cfg_info[index].name);
			ms_errRet(-2,"%s",poutstr);
		}
	}

	if(right_num!=pnetcardinfo->num){
		ms_sprintfs(poutstr, "Iner error(right_num!=pnetcardinfo->num(%d,%d)),maybe iface name error",right_num,pnetcardinfo->num);
		ms_errRet(-2,"%s",poutstr);
	}else{
		netcardinfo_db.num=bond_index;
	}
	if(ms_false==msifconfig_innerapi_checkDefaultGW( poutstr)){
		ms_errRet(-3,"msifconfig_innerapi_checkDefaultGW failed");
	}
	
	if(ossys_rhel_centos==os_info.ossystem
		||ossys_rhel==os_info.ossystem){
		msifconfig_innnerapi_setRhel(pnetcardinfo, poutstr);
	}else if(ossys_debain_ubuntu1604==os_info.ossystem
			||ossys_debain_ubuntu1804==os_info.ossystem
			||ossys_debain_ubuntu2004==os_info.ossystem
			||ossys_debain_deepin==os_info.ossystem
			||ossys_debain_uos20==os_info.ossystem){
		msifconfig_innerapi_setIfupdown(pnetcardinfo, poutstr);
	}else{
		msifconfig_innerapi_setNetplan(pnetcardinfo, poutstr);
	}
	msifconfig_innerapi_writedb();
	return 0;
}

ms_s08 msifconfig_api_restart(ms_u08 * ms_out  poutstr)
{
	if(ms_false==pnetcard_info->isinit||ms_false==netcardinfo_db.isinit){
		ms_sprintfs(poutstr, "%s","Init pnetcard_info and netcardinfo_db first");
		ms_errRet(-1,"%s",poutstr);
	}
	ms_u08 netcard_index=0;
	ms_bool need_restart_alliface=ms_false;
	ms_s08 cmdline[256]={0};
	ms_s08 ret =0;
	ms_bool flag_recovry=ms_false;
	for(netcard_index=0;netcard_index<netcardinfo_db.num;netcard_index++){
		NETCFGInfo *pcfg_info=&netcardinfo_db.cfg_info[netcard_index];
		if(ms_true==pcfg_info->flag_needRestart){
			flag_recovry=ms_true;
			if(ossys_rhel_centos==os_info.ossystem
				||ossys_rhel==os_info.ossystem
				||ossys_debain_ubuntu1604==os_info.ossystem
				||ossys_debain_ubuntu1804==os_info.ossystem
				||ossys_debain_ubuntu2004==os_info.ossystem
				||ossys_debain_deepin==os_info.ossystem
				||ossys_debain_uos20==os_info.ossystem){
				//restart one
				ret=msifconfig_innerapi_restartIfupdown(pcfg_info,  poutstr);
				if(ret<0){
					goto ms_end;
				}
				pcfg_info->flag_needRestart=ms_false;
			}else{
				//restart all
				ret=msifconfig_innerapi_restartNetplan(poutstr);
				pcfg_info->flag_needRestart=ms_false;
				goto ms_end;
			}
		}
	}
ms_end:
	if(ms_true==flag_recovry){
		msifconfig_innerapi_updateData();
	}
	return ret;
}
ms_void msifconfig_api_buildJsonInfo(ms_void * jsnode_item)
{
	if(ms_false==pnetcard_info->isinit||ms_false==netcardinfo_db.isinit){
		ms_errNoret("%s","Init pnetcard_info and netcardinfo_db first");
	}
	cJSON * jsnode=(cJSON *)jsnode_item;
	ms_u08 netcard_index=0;
	cJSON * json_netcard_info_array = cJSON_CreateArray();
	cJSON_AddItemToObject(jsnode, "netcard_info", json_netcard_info_array);
	for(netcard_index=0;netcard_index<netcardinfo_db.num;netcard_index++){
		cJSON * json_netcard_info=cJSON_CreateObject();
		cJSON_AddItemToArray(json_netcard_info_array,json_netcard_info);
		MSJSON_BUILD_STRING(json_netcard_info, "name",netcardinfo_db.cfg_info[netcard_index].name) ;
		MSJSON_BUILD_STRING(json_netcard_info, "nameAnother",netcardinfo_db.cfg_info[netcard_index].nameAnother) ;
		MSJSON_BUILD_STRING(json_netcard_info, "address",netcardinfo_db.cfg_info[netcard_index].address) ;
		MSJSON_BUILD_STRING(json_netcard_info, "ipaddress",netcardinfo_db.cfg_info[netcard_index].ipaddress) ;
		MSJSON_BUILD_STRING(json_netcard_info, "netmask",netcardinfo_db.cfg_info[netcard_index].netmask) ;
		MSJSON_BUILD_STRING(json_netcard_info, "gateway",netcardinfo_db.cfg_info[netcard_index].gateway) ;
		MSJSON_BUILD_STRING(json_netcard_info, "mtu",netcardinfo_db.cfg_info[netcard_index].mtu) ;
		MSJSON_BUILD_STRING(json_netcard_info, "bondmaster_name",netcardinfo_db.cfg_info[netcard_index].bondmaster_name) ;
		MSJSON_BUILD_STRING(json_netcard_info, "bondslave_name",netcardinfo_db.cfg_info[netcard_index].bondslave_name) ;
		MSJSON_BUILD_STRING(json_netcard_info, "dns1",netcardinfo_db.cfg_info[netcard_index].dns1) ;
		MSJSON_BUILD_STRING(json_netcard_info, "dns2",netcardinfo_db.cfg_info[netcard_index].dns2) ;
		MSJSON_BUILD_STRING(json_netcard_info, "mode",netcardinfo_db.cfg_info[netcard_index].mode) ;
		cJSON_AddNumberToObject(json_netcard_info, "flag_defaultgw",netcardinfo_db.cfg_info[netcard_index].flag_defaultgw) ;
	}
}
ms_void msifconfig_api_parseJsonInfo(ms_void *jsnode_item,NETCARDInfo *pNetcardInfo)
{
	if(ms_false==pnetcard_info->isinit||ms_false==netcardinfo_db.isinit){
		ms_errNoret("%s","Init pnetcard_info and netcardinfo_db first");
	}
	cJSON * js_root=(cJSON *)jsnode_item;
	cJSON *js_ifaceinfo_arry=cJSON_GetObjectItem(js_root,"iface_info");
	if(ms_null!=js_ifaceinfo_arry){
		ms_memset(pNetcardInfo,0,sizeof(NETCARDInfo));
		pNetcardInfo->num=cJSON_GetArraySize(js_ifaceinfo_arry);
		cJSON *js_ifaceinfo=js_ifaceinfo_arry->child;
		cJSON *js_item= ms_null;
		int index_ifaceinfo=0;
		while(ms_null!=js_ifaceinfo){
			NETCFGInfo *pcfginfo=&pNetcardInfo->cfg_info[index_ifaceinfo];
			MSJSON_PARSE_STRING(js_item,js_ifaceinfo,"name",pcfginfo->name);
			MSJSON_PARSE_STRING(js_item,js_ifaceinfo,"nameAnother",pcfginfo->nameAnother);
			MSJSON_PARSE_STRING(js_item,js_ifaceinfo,"address",pcfginfo->address);
			MSJSON_PARSE_STRING(js_item,js_ifaceinfo,"ipaddress",pcfginfo->ipaddress);	
			MSJSON_PARSE_STRING(js_item,js_ifaceinfo,"netmask",pcfginfo->netmask);
			MSJSON_PARSE_STRING(js_item,js_ifaceinfo,"gateway",pcfginfo->gateway);
			MSJSON_PARSE_STRING(js_item,js_ifaceinfo,"mtu",pcfginfo->mtu);	
			MSJSON_PARSE_STRING(js_item,js_ifaceinfo,"dns1",pcfginfo->dns1);	
			MSJSON_PARSE_STRING(js_item,js_ifaceinfo,"dns2",pcfginfo->dns2);
			MSJSON_PARSE_STRING(js_item,js_ifaceinfo,"bondmaster_name",pcfginfo->bondmaster_name);
			MSJSON_PARSE_STRING(js_item,js_ifaceinfo,"bondslave_name",pcfginfo->bondslave_name);
			ms_verbose("--------netcardinfo-%02d-%02d-%s",pNetcardInfo->num,index_ifaceinfo,pcfginfo->name);
			MSJSON_PARSE_STRING(js_item,js_ifaceinfo,"mode",pcfginfo->mode);	
			MSJSON_PARSE_INT(js_item,js_ifaceinfo,"flag_defaultgw",pcfginfo->flag_defaultgw);	
			pcfginfo->flag_needRestart=ms_true;
			index_ifaceinfo++;
			js_ifaceinfo=js_ifaceinfo->next;  
		}
	}
}
#undef MSIFCONFIG_C
